package com.limboline.bssystem.controller;

import com.limboline.bssystem.entity.CommodityEntity;
import com.limboline.bssystem.service.CommodityService;
import com.limboline.bssystem.service.CustomerService;
import com.limboline.bssystem.util.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;


@RequestMapping("/")
@Controller
public class CommodityController {

    @Autowired
    CommodityService commodityService;

    @Autowired
    CustomerService customerService;

    @RequestMapping("/login")
    public ModelAndView login(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("/logout")
    public ModelAndView logout(){
        State.getInstance().setState(0);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("/account")
    @ResponseBody
    public String check(@RequestParam String userName, @RequestParam String password){
        return commodityService.check(userName, password);
    }

    @RequestMapping("/")
    public ModelAndView index(@RequestParam(required = false) Integer type){
        ModelAndView modelAndView = new ModelAndView();
        commodityService.edit(modelAndView, type);
        switch (State.getInstance().getState()){
            case 0: modelAndView.setViewName("index"); return modelAndView;
            case 1: modelAndView.setViewName("indexSeller"); return modelAndView;
            case 2: modelAndView.setViewName("indexBuyer"); return modelAndView;
        }
        return modelAndView;
    }

    @RequestMapping("/show")
    public ModelAndView show(@RequestParam Integer id){
        ModelAndView modelAndView = new ModelAndView();
        int type = State.getInstance().getState();
        commodityService.show(modelAndView, type, id);
        switch (type){
            case 0: modelAndView.setViewName("detail"); return modelAndView;
            case 1: modelAndView.setViewName("detailSeller"); return modelAndView;
            case 2: modelAndView.setViewName("detailBuyer"); return modelAndView;
        }
        return modelAndView;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam Integer id){
        return commodityService.delete(id) ? "success" : "error";
    }

    @RequestMapping("/modify")
    public ModelAndView modify(@RequestParam Integer id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("publish");
        commodityService.show(modelAndView, 0, id);
        return modelAndView;
    }

    @RequestMapping("/publish")
    public ModelAndView publish(){
        if(State.getInstance().getState() == 1){
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("commodity", new CommodityEntity());
            modelAndView.setViewName("publish");
            return modelAndView;
        }else{
            return logout();
        }
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return commodityService.uploadImg(multipartFile) ? "success" : "error";
    }

    @RequestMapping("/submit")
    @ResponseBody
    public String submit(@RequestBody String data){
        return commodityService.submitCommodity(data).toString();
    }

    @RequestMapping("/success")
    public ModelAndView publishSuccess(@RequestParam Integer id, @RequestParam Boolean isFirst){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("id", id);
        if(isFirst){
            modelAndView.addObject("content", "发布成功");
        }else{
            modelAndView.addObject("content", "编辑成功");
        }
        return modelAndView;
    }

    @RequestMapping("/buy")
    @ResponseBody
    public String buy(@RequestBody String data){
        return customerService.buy(data) ? "success" : "error";
    }

    @RequestMapping("/addOrder")
    public ModelAndView addOrder(@RequestParam Integer id, @RequestParam Integer num){
        customerService.addOrder(id, num);
        return index(State.getInstance().getState());
    }

    @RequestMapping("/bill")
    public ModelAndView bill(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("bill");
        customerService.listBill(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/preorder")
    public ModelAndView preOrder(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("preOrder");
        customerService.listOrder(modelAndView);
        return modelAndView;
    }
}
